package com.example.dps;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;


/**
 * Constants needed for all programs 
 * @author merrickc
 *
 */



public class Utility {
	
	public static final int LOGIN_DIALOG = 123;
	
	
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
    

	public static void adjustBackground(Activity activity){
			
			DisplayMetrics displaymetrics = new DisplayMetrics();
			activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
			int height = displaymetrics.heightPixels;
			int width = displaymetrics.widthPixels;
			
			int screenWidth = 0;
			
			Resources resources = activity.getResources();
			
			Drawable unscaled = resources.getDrawable(R.drawable.content_bk_slice);
			
			Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.content_bk_slice);
			
			bitmap = Bitmap.createScaledBitmap(bitmap, width, bitmap.getHeight(), false);
			
			BitmapDrawable scaled = new BitmapDrawable(activity.getResources(), bitmap);
			
			scaled.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
			
			LinearLayout backgroundLayout = (LinearLayout) activity.findViewById(R.id.backgroundLayout);
			
			backgroundLayout.setBackgroundDrawable(scaled);
			
		}
	
	public static Drawable returnAdjustedBackgroundDrawable(Activity activity){
			
		DisplayMetrics displaymetrics = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		int height = displaymetrics.heightPixels;
		int width = displaymetrics.widthPixels;
		
		int screenWidth = 0;
		
		Resources resources = activity.getResources();
		
		Drawable unscaled = resources.getDrawable(R.drawable.content_bk_slice);
		
		Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.content_bk_slice);
		
		bitmap = Bitmap.createScaledBitmap(bitmap, width, bitmap.getHeight(), false);
		
		BitmapDrawable scaled = new BitmapDrawable(activity.getResources(), bitmap);
		
		scaled.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
		
		return scaled;
			
	}
	
	public static String md5(String s) {
	    try {
	        // Create MD5 Hash
	        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
	        digest.update(s.getBytes());
	        byte messageDigest[] = digest.digest();

	        // Create Hex String
	        StringBuffer hexString = new StringBuffer();
	        for (int i=0; i<messageDigest.length; i++)
	            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
	        return hexString.toString();

	    } catch (NoSuchAlgorithmException e) {
	        e.printStackTrace();
	    }
	    return "";
	}
	
}
    
